Fix the matplotlib backend for qc plotting#797
Merged
m-reuter merged 2 commits intoDeep-MI:devfrom Mar 7, 2026
Merged
Conversation
c4fd69a to
56aa1ed
Compare
Changes
- Add `noop_context` and `backend` context manager for safe matplotlib backend switching FastSurferCNN/utils/__init__.py
- Use `backend('agg')` context to fix --qc_snaps failure when qt unavailable in HypVINN/utils/visualization_utils.py, CorpusCallosum/shape/endpoint_heuristic.py, CorpusCallosum/shape/postprocessing.py and CorpusCallosum/utils/visualization.py
56aa1ed to
60a50d5
Compare
Contributor
There was a problem hiding this comment.
Pull request overview
This PR aims to make QC snapshot/plot generation more robust in headless environments by introducing a Matplotlib-backend context manager and using it in HypVINN and CorpusCallosum plotting paths (instead of relying on whatever the default GUI backend is).
Changes:
- Add
FastSurferCNN.utils.plotting.backend()context manager to temporarily switch Matplotlib backends. - Use the backend context for HypVINN QC snapshot generation and CorpusCallosum plotting/debug plotting.
- Update HypVINN
run_prediction.pyexecution flow (thread executor usage + improved QC future error reporting).
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| HypVINN/utils/visualization_utils.py | Wrap QC snapshot figure creation/saving in a forced agg backend context; refactor cropping logic. |
| HypVINN/run_prediction.py | Switch executor selection logic and improve QC snapshot async error handling/logging. |
| FastSurferCNN/utils/plotting.py | New backend-switching context manager utility. |
| FastSurferCNN/utils/init.py | Export new noop_context utility. |
| CorpusCallosum/utils/visualization.py | Use backend context (or no-op) for plotting depending on whether saving is requested. |
| CorpusCallosum/shape/postprocessing.py | Replace manual backend switching with backend context for interactive debug plots. |
| CorpusCallosum/shape/metrics.py | Replace manual backend switching with backend context for interactive debug plots. |
| CorpusCallosum/shape/endpoint_heuristic.py | Replace manual backend switching with backend context for interactive debug plots + minor plotting arg tweaks. |
| CorpusCallosum/segmentation/segmentation_postprocessing.py | Replace manual backend switching with backend context for interactive debug plots. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…and use the global executor instances instead of local instances - Only import matplotlib.pyplot after backend context switches to avoid unavailable backend errors - Log warnings if qc image generation failed in HypVINN Clean up formatting
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
HypVINN creates qc snapshots, if the
--qc__snapsfalg is passed, but this uses the "default" backend of matplotlib.So qc_snapshot generation fails, when the default backend is not available, e.g. default qtagg is not available because qt is not installed/available.
To resolve, we force the add matplotlib backend with a context manager.
The contextmanager solution is also propagated into the CorpusCallosum module, where a similar problem was addressed with manual backend switches.